//
//  LGTTaskTypeSelectView.swift
//  KJLGT
//
//  Created by jenkin on 2019/9/9.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit

class LGTTaskTypeSelectView: UIView {
    enum Position:Int {
        case Left = 0
        case Right = 1
        case Center = 2
    }
    
    var conditionListModel:ConditionListModel?
    var currentConditionModel:ConditionModel?
    var clickBlock:ClickIndexWithIndexBlock?
    var index:Int = 0
    var selectButton:LGTTaskTypeSelectButton?
    
    static func initWithPosition(positon:Position, conditionListModel:ConditionListModel, index:Int) -> LGTTaskTypeSelectView{
        let selectView:LGTTaskTypeSelectView = LGTTaskTypeSelectView()
//        selectView.backgroundColor = UIColor.orange
        selectView.conditionListModel = conditionListModel
        selectView.index = index
        selectView.selectButton = selectView.getTypeSelectView(title: conditionListModel.title ?? "", tag: index)
//        button.backgroundColor = UIColor.red
        selectView.addSubview(selectView.selectButton!)
        if positon == .Left{
            selectView.selectButton!.snp.makeConstraints { (make) in
                make.left.bottom.top.equalTo(0)
                make.width.equalTo(SizeFit(100))
            }
        }else if positon == .Right{
            selectView.selectButton!.snp.makeConstraints { (make) in
                make.right.bottom.top.equalTo(0)
                make.width.equalTo(SizeFit(100))
            }
        }else if positon == .Center{
            selectView.selectButton!.snp.makeConstraints { (make) in
                make.bottom.top.equalTo(0)
                make.centerX.equalTo(selectView)
                make.width.equalTo(SizeFit(80))
            }
        }
        return selectView
    }
    
    func getTypeSelectView(title:String, tag:Int) -> LGTTaskTypeSelectButton{
        //sanjiao drop_down retract
        let selectView:LGTTaskTypeSelectButton = LGTViewCreateUtil.share.createTaskTypeSelectButton(position: LGTTaskTypeSelectButton.Position.right, spaceX:SizeFitFloat(5), title: title, image:"sanjiao", selectImage:"sanjiao", font: Font_Normal_15, color: TextColor, selectedColor: TextColor, dH:5)
        selectView.tag = tag
        selectView.addTarget(self, action: #selector(selectAction), for: UIControl.Event.touchUpInside)
        return selectView
    }
    
    @objc func selectAction(sender:LGTTaskTypeSelectButton){
        self.changeSelectState(select:!sender.isSelected)
        if self.clickBlock != nil{
            self.clickBlock!(self.tag, self.index)
        }
    }
    
    func changeSelectState(select:Bool){
        if self.selectButton != nil{
            self.selectButton!.isSelected = select
        }
    }
    
    func selelctRow(_ row:Int){
        let title:String = self.currentConditionModel?.name ?? (self.conditionListModel?.title ?? "")
        self.selectButton?.setTitle(title, for: UIControl.State.normal)
        self.selectButton?.setTitle(title, for: UIControl.State.selected)
    }
    
    func setTitle(title:String, isSelected:Bool = false){
        self.selectButton?.setTitle(title, for: UIControl.State.normal)
        self.selectButton?.setTitle(title, for: UIControl.State.selected)
        self.selectButton?.isSelected = isSelected
    }
}
